package com.ruoyi.system.service.impl.video;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.video.VsDramasCategory;
import com.ruoyi.system.domain.video.VsDramasVideoFavorite;
import com.ruoyi.system.domain.video.VsDramasVideoLog;
import com.ruoyi.system.mapper.video.VsDramasCategoryMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoFavoriteMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasVideoMapper;
import com.ruoyi.system.domain.video.VsDramasVideo;
import com.ruoyi.system.service.video.IVsDramasVideoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 短剧Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class VsDramasVideoServiceImpl implements IVsDramasVideoService 
{
    @Autowired
    private VsDramasVideoMapper vsDramasVideoMapper;

    @Autowired
    private VsDramasCategoryMapper vsDramasCategoryMapper;

    @Autowired
    private VsDramasVideoFavoriteMapper vsDramasVideoFavoriteMapper;

    /**
     * 查询短剧
     * 
     * @param id 短剧主键
     * @return 短剧
     */
    @Override
    public VsDramasVideo selectVsDramasVideoById(Integer id)
    {
        return vsDramasVideoMapper.selectVsDramasVideoById(id);
    }

    /**
     * 查询短剧列表
     * 
     * @param vsDramasVideo 短剧
     * @return 短剧
     */
    @Override
    public List<VsDramasVideo> selectVsDramasVideoList(VsDramasVideo vsDramasVideo)
    {
        VsUser user = null;
        VsDramasVideoFavorite favorite = new VsDramasVideoFavorite();
        try {
            user = SecurityUtils.getLoginUser().getVsUser();
            favorite.setUserId(user.getId());
        } catch (Exception e) {

        }
        List<VsDramasVideo> vsDramasVideos = vsDramasVideoMapper.selectVsDramasVideoList(vsDramasVideo);
        for (VsDramasVideo v : vsDramasVideos) {
            v.setSumfavorites(formatAndSet(v.getFavorites() , v.getFakeFavorites()));
            v.setSumviews(formatAndSet(v.getViews() , v.getFakeViews()));
            v.setSumshares(formatAndSet(v.getShares() , v.getFakeShares()));
            v.setSumlikes(formatAndSet(v.getLikes() , v.getFakeLikes()));
            v.setSumhots(formatAndSet(Math.toIntExact(v.getHots()), 0));
            List<VsDramasCategory> categoryNames = new ArrayList<>();
            String[] parts = v.getCategoryIds().split(",");
            for (int i = 0; i < parts.length; i++) {
                int parseInt = Integer.parseInt(parts[i]);
                VsDramasCategory category = vsDramasCategoryMapper.selectVsDramasCategoryById(parseInt);
                if (category != null) {
                    categoryNames.add(category);
                }
            }
            if (!categoryNames.isEmpty()) {
                v.setCategoryNames(categoryNames);
            }
            String[] split = v.getFlags().split(",");
            List<String> flagList = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                if ("hot".equals(split[i])) {
                    flagList.add("热门");
                } else if ("recommend".equals(split[i])) {
                    flagList.add("推荐");
                }
            }
            if (!flagList.isEmpty()) {
                v.setFlagList(flagList);
            }
            if (user != null) {
                favorite.setType("favorite");
                favorite.setVid(Long.valueOf(v.getId()));
                List<VsDramasVideoFavorite> favorites = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(favorite);
                if (favorites.size() > 0) {
                    v.setFavorite(true);
                } else {
                    v.setFavorite(false);
                }
            }
        }
        return vsDramasVideos;
    }

    private String formatAndSet(int originalValue, int fakeValue) {
        int sum = originalValue + fakeValue;
        if (sum > 1000) {
            double formattedValue = (double) sum / 1000;
            return String.format("%.2fk", formattedValue);
        } else {
            return sum + "k";
        }
    }

    /**
     * 新增短剧
     * 
     * @param vsDramasVideo 短剧
     * @return 结果
     */
    @Override
    public int insertVsDramasVideo(VsDramasVideo vsDramasVideo)
    {
        vsDramasVideo.setCreatetime(System.currentTimeMillis());
        vsDramasVideo.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoMapper.insertVsDramasVideo(vsDramasVideo);
    }

    /**
     * 修改短剧
     * 
     * @param vsDramasVideo 短剧
     * @return 结果
     */
    @Override
    public int updateVsDramasVideo(VsDramasVideo vsDramasVideo)
    {
        vsDramasVideo.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoMapper.updateVsDramasVideo(vsDramasVideo);
    }

    /**
     * 批量删除短剧
     * 
     * @param ids 需要删除的短剧主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoByIds(Integer[] ids)
    {
        return vsDramasVideoMapper.deleteVsDramasVideoByIds(ids);
    }

    /**
     * 删除短剧信息
     * 
     * @param id 短剧主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoById(Integer id)
    {
        return vsDramasVideoMapper.deleteVsDramasVideoById(id);
    }
    @Transactional
    @Override
    public void batchUpdateVideo(VsDramasVideo vsDramasVideo) {
        vsDramasVideo.setUpdatetime(System.currentTimeMillis());
        if(vsDramasVideo.getPrice() != null) {
            vsDramasVideoMapper.batchUpdateVideo(vsDramasVideo);
        }
        if (vsDramasVideo.getVprice() != null) {
            vsDramasVideoMapper.batchUpdateOtherVideo(vsDramasVideo);
        }

        vsDramasVideoMapper.batchUpdateEpisodes(vsDramasVideo);
        vsDramasVideoMapper.batchUpdateOtherEpisodes(vsDramasVideo);
    }
}
